<template>
  <div class="uploadimg">
    <el-upload
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :http-request="httpRequest"
    >
      <img
        v-if="value"
        :src="value"
        class="avatar"
        v-imgError="errorImg"
        @click.stop="isShow = true"/>
      <i v-else class="el-icon-plus avatar-uploader-icon"
    /></el-upload>
    <!-- 关闭按钮 -->
    <div class="close" @click="del" v-if="value">X</div>
    <!-- 查看大图 -->
    <el-dialog :visible.sync="isShow" title="大图预览" width="400px">
      <img :src="value" style="width:100%" alt="" />
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDObYFf5F9m7C0XnnWy94h38wTjiQr0sTn',
  SecretKey: '4rOedKkvqUrwlJZYGhrkMhkW1GOGrBF2'
})
export default {
  name: 'Uploadimg',
  props: {
    value: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: false,
      errorImg: require('@/assets/common/head.jpg')
    }
  },
  created () {
    console.log('this.value', this.value)
  },
  methods: {
    httpRequest (data) {
      console.log(data)
      cos.putObject(
        {
          Bucket: 'dhf481229-1305249343' /* 存储桶 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: data.file.name /* 标识 */,
          StorageClass: 'STANDARD', // 固定值
          Body: fileObject, // 上传文件对象  file文件对象
          onProgress: progressData => {
            // 上传进度，实时返回上传文件进度
            console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          // 上传成功或者失败处理
          // err为空就是成功，err有值就是失败了
          // data就是上传成功后腾讯云返回的数据（上传文件地址）
          console.log(err || data)
          this.imageUrl = 'http://' + data.Location
        }
      )
    },
    del () {}
  }
}
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader .el-upload {
  background-color: #fff;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.uploadImg {
  width: 178px;
  height: 178px;
  position: relative;
  &:hover {
    .close {
      display: block;
    }
  }
  .close {
    display: none;
    position: absolute;
    top: 5px;
    right: 5px;
    cursor: pointer;
    color: #eee;
    font-size: 20px;
  }
  .el-progress {
    text-align: center;
  }
  .depa-enter-active,
  .depa-leave-active {
    transition: all 0.5s;
  }
  .depa-enter,
  .depa-leave-to {
    opacity: 0;
  }
}
</style>
